$(function(){
		var dataCount = 0;
		var totalHeight =0;
		setTimeout(function(){
	   		$("#nav").fadeIn(1000);
	   	},2000)
	   	function navPosition(){
	   		var wx = $(window).width() / 2 -59;
	   		$("#nav").css("left",wx);
	   	}
	   	navPosition();
	   
	   	$(window).resize(function(){
	   		navPosition();
	   	});
		$("h1").on("click",function(){
			location.href = "index3.html";
		});
		$("#goback").click(function(){
			history.go(-1);
		});
		setTimeout(function(){
			$("#searchDiv").fadeIn().find(".ip").animate({width:"250px"},1500,function(){
				$("h1").show().addClass("animated bounceInLeft");
			});
		},1000);
		var timer = "";
		var scollArg = "";
		$(".ip").on("click",function(event){
			event.stopImmediatePropagation();
			$("h1").hide();			
			$("#searchDiv").animate({top:0},function(){
				$("h1").hide();	
				$(".dataCount").fadeIn();
				$(this).css({background:"rgba(173,216,230,0.4)"}).find(".ip").css({border:0}).find("a").css({background:"rgba(255,48,48,0.6)",borderRadius:0,width:"34px",height:"34px",right:"8px",top:"4px"});
				$("#searchDiv ul").slideDown();
				$(".ip").prev().fadeIn();
					$("#searchBtn").click(function(event){
						event.stopImmediatePropagation();
						$("#recommend").slideUp();
//						$(".search-content").slideDown();
						$("#data-content").slideDown();
						totalHeight = $("#data-content")[0].scrollHeight;
	  					 console.log(totalHeight);
						var data = $("input#search").val();
						$.ajax({
							type:"post",
							url:"/moveSection",
							async:true,
							data:data
						}).done(function(status,resData,jqXHR){
							if(resData){
								var htmlStr = template("mainTemplate",resData);
								$("#data-content").html(htmlStr);
							}else{
								$("recommend").slideUp().html('<li style="width: 100%;height: 100px;text-align: center;background-color: white;line-height: 100px;border-radius: 0 0 5px 5px;">找不到相关的搜索内容</li>');				
							}							
						}).fail(function(jqXHR,status,error){
							console.log(error)
						});
					});
//				$("html,body").css("overflow","scroll");
//			$(window).on("scroll",function(){				
////				scollArg = arguments;
//			
//			})
		})
		$("input#search").on("input",function(){
			var data = $(this).val();
			$.post("/keywords",data,function(resData,status){
				if(resData){
					var htmlStr = template("recommendSection",resData);
					$("#recommend").html(htmlStr);
				}else{
					$("#recommend").html('<li style="width: 100%;height: 100px;text-align: center;background-color: white;line-height: 100px;border-radius: 0 0 5px 5px;">找不到相关的搜索内容</li>');
				}
			});
		})
		// 获取推荐主题
		$.ajax({
			type:"post",
			url:"/search",
			async:true
		}).done(function(resData,status,jqXHR){
			if(resData.length){
				var htmlStr = template("recommendSection",resData);
				$("#recommend").html(htmlStr);
			}
		}).fail(function(jqXHR,status,error){
			console.log(error);
		});
		
		$("body").click(function(event){
			  	event.stopImmediatePropagation();
				$("#searchDiv ul").slideUp();
			
		})
		
		
		// 回到顶部按钮事件
		$("#backTop").click(function(event){
			$("#data-content").animate({scrollTop:0},500);
			$("html,body").animate({scrollTop:0})
		});
		// 到底部按钮事件
		$("#backBottom").click(function(){
			var vh = $("#data-content")[0].scrollHeight-$(window).height();			
			$("#data-content").animate({scrollTop:vh},500);
			$("html,body").animate({scrollTop:vh})
		});
		// 点击搜索栏下面的推荐栏事件
		$("#recommend").on("click","li",function(){
			$("#recommend").slideUp();
//			$(".search-content").slideDown();
			$("#data-content").slideDown();
			 totalHeight = $("#data-content")[0].scrollHeight;
	   		console.log(totalHeight);
			var data = $(this).prop("data-sectionId");
			$.ajax({
				type:"post",
				url:"/moveSection",
				async:true,
				data:data
			}).done(function(status,resData,jqXHR){
				var htmlStr = template("mainTemplate",resData);
				$("")
			}).fail(function(jqXHR,status,error){
				console.log(error)
			});
		});		
//		$(".shortcutBtn").hover(function(){					
//					clearTimeout(timer);					
//		},function(){
//				$(".shortcutBtn").fadeOut();				
//		});
		
//		     var jsonObject1 = {
//            "name": "xiaoming",
//           "age": 29
//       	}
//       var keys1 = [];
//   for (var p1 in jsonObject1) {
//       if (jsonObject1.hasOwnProperty(p1))
//           keys1.push(p1);
//   }		
	var total = 0;
	   	// 下拉加载
	   		$("#data-content").on("scroll",function(){	
			var sT = $("#data-content").scrollTop();
			var height = $("#data-content").height();
			 total = sT+height;
				setTimeout(function(){
					$("#searchDiv ul").slideUp();
				},2000)			
				if($("#data-content").scrollTop()){
					clearTimeout(timer);
					$(".shortcutBtn").fadeIn();
					 timer = setTimeout(function(){
						$(".shortcutBtn").fadeOut();				
					},1000);	
				}				
				});
				if(total>=totalHeight-10){
//				sendFCs();
				$(".loading").css({backgroundImage:"url(../images/loading_010.gif)",backgroundRepeat:"no-repeat",backgroundPosition: "50% 0"}).find("div").hide();	

					
					$.ajax({
						type:"get",
						url:"/newData",
						async:true,
						data:dataCount
					}).done(function(resData){
						if(resData){							
							htmlStr = template("mainTemplate",resData);
   							$("#data-content").append(htmlStr);
   							$(".loading").appendTo("#data-content");
   							dataCount = dataCount+resData.length;
   							$(".dataCount").text(dataCount);
							$(".loading").css("background-image","").find("div").show().find(".iconM").show().next().text("上拉加载"); 						
						}else{							
							$(".loading").css("background-image","").find("div").show().html("<p>没有更多的数据</p>");					
						}
					}).fail(function(jqXHR,status,error){
						console.log(error);
					})
			}
				
			});
	   	
	})